<?php
//企业名片首页
define('IN_T', true);
require 'include/init.php';

//首页展示全景
$c_id = intval($_REQUEST['cid']);
$tp->assign('c_id',$c_id);

//读取商户基本信息
$sql = "SELECT c.* FROM " . $Base->table('company_profile') . " as c " .
 	   "WHERE c.c_id = $c_id";

$company_profile = $Db->query($sql, "Row");

//非法访问，跳转网站首页
if (empty($company_profile)) {
    echo '商家不存在或未完善基本信息！';
    exit;
}

//整合营业时间
$open_time = explode(',', $company_profile['open_time']);
$company_profile['open_start'] = $open_time[0];
$company_profile['open_end'] = $open_time[1];
$pano_url = explode('?',$company_profile['pano_url']);
$company_profile['pano_uuid'] = $pano_url[0];
if(!empty($pano_url[1])){
	$pano_url_scene = explode('=',$pano_url[1]);
	$company_profile['pano_scene'] = $pano_url_scene[1];
}

$company_profile['brief_share'] = preg_replace("/\s/","", Common::sfilter($company_profile['brief']));
//商户信息
$tp->assign('company_profile', $company_profile);

//读取自己的栏目分类
$cats = productCatList($c_id);
$tp->assign('cats', $cats);

//提取商户相册 
$sql = "select id,img_name,img_path from " . $Base->table('company_thumb') . " where c_id=$c_id order by sort asc,id asc";
$thumbs = $Db->query($sql);
$tp->assign('thumbs', $thumbs);

//提取附近景区
$sql = "select * from " . $Base->table('company_nearby') . " where c_id=$c_id order by sort asc, id asc";
$nearby = $Db->query($sql);
foreach ($nearby as $k => $v) {
    $nearby[$k]['lnglat'] = explode(',', $v['position']);
}
$tp->assign('nearby', $nearby);

//提取新闻
$sql = "select * from " . $Base->table('company_news') . " where cat_type=0 and c_id=$c_id order by id desc";
$news = $Db->query($sql);
foreach ($news as $k => $v) {
    $pics = Common::get_pics_from_html($v['content']);
    $news[$k]['pic'] = $pics[0];
}
$sql = "SELECT t.notice,t.href FROM " . $Base->table('tag_works') . " as tw " .
       "left join" . $Base->table("tag") . " as t on t.id=tw.tag_id " .
       "WHERE tw.works_id = " . $company_profile["pk_works_main"] . " and t.notice <> '' ".
	   "and t.time_s<='".date('Y-m-d',Common::gmtime())."' and t.time_e>='".date('Y-m-d',Common::gmtime())."' ".
	   "order by t.sort asc, t.id desc";
$temp_data = $Db->query($sql);
if (!empty($temp_data) && !empty($temp_data[0]["notice"])) {
    $tp->assign("notice", $temp_data[0]);
} else {
    //读取自己的
    if (!empty($company_profile["notice"])) {
        $tp->assign("notice", array("notice" => $company_profile["notice"], "href" => $company_profile["href"]));
    }
}
//print_r($news);
$tp->assign('news', $news);

$tp->display('index.tpl');

function productCatList($c_id)
{
    $sql = "SELECT new.*,admin.name,admin.icon FROM " . $GLOBALS['Base']->table('company_new_cat') ." as new " .
        'left join'.$GLOBALS['Base']->table('company_admin_new_cat').' as admin on admin.id=new.pid '.
        'where new.c_id='.$c_id." ".
        "ORDER BY  sort asc , id asc";
    $list = $GLOBALS['Db']->query($sql);
    return $list;
}
?>